import { request } from '../network/request.js'
import router from '../router/index'

export default ({
  namespaced: true,
  state: {
    all: {
      producdata: [],
      searchchoose: "",//查询字段
      select: "",//查询类型
      currentPage: "2",
      pagesize: "2",
      totals: -1
    },
     
    product: {},
    goodsshow:{}//上传数据

  },
  mutations: {//改变仓库数据
    changeproducdata(state, producdata) {
      state.all.producdata = producdata
    },
    changeproductotals(state, totals) {
      state.all.totals = totals
    },
    changeselect(state,data){
      state.all.select=data.select
      state.all.searchchoose=data.searchchoose
    },

  },
  actions: {
    //渲染所有库存数据
    async getproduc(context, { pagesize, currentPage,select,searchchoose }) {
      console.log(pagesize, currentPage,select,searchchoose);
      const msg = await request({
        url: "/stroe/getproduct",//商品二级路经
        method: "get",
        params: {
          pagesize,
          currentPage,
          searchchoose,
          select
        }//传参 
      })
      //console.log(msg)
      const { data } = msg
      context.commit("changeproducdata", data)
      var totals = msg.totals
      context.commit("changeproductotals", totals)
    },
    //删除指定库存商品
    async renoveone(context, id) {
      //console.info("删除", id)
      const data = await request({

        url: "/stroe/renoveone",
        method: "post",
        data: { _id: id }
      })
      //console.log('删除商品',data);
      context.dispatch('getproduc',context.state.all);
    
    },

    //商品的增加
    async addproduc(context, addone) {
      var msg = await request({
        url: "/stroe/addproduc",
        method: "post",
        data: addone,
      })
     if(msg.msg=='新增成功'){
          router.push('/system/productlist')
     }
    },
    //获取修改商品的ID  getupdataproductID 
    async getupdataproductID(context, id) {
      var datas = await request({
        url: "/stroe/getupdataproductID",
        method: "post",
        data: { id }
      })
      context.state.product = datas[0]
     // console.log( 'hhhh',context.state.product, datas);
    },
    async updataproduct(context, product) {
      var datas = await request({
        url: "/stroe/updataproduct",
        method: "post",
        data: product
      })
     // console.info(datas.msg)
    },

    //搜索商品
    async search(context, searchdata) {
      console.log(searchdata);
      context.commit("changeselect", searchdata);
      context.dispatch('getproduc',context.state.all)
      // var datas = await request({
      //   url: "/stroe/getproduct",
      //   method: "get",
      //   params: {
      //     searchchoose: searchdata.searchchoose,
      //     select: searchdata.select,

      //   }//传参 
      //})
     // console.info("搜索", datas)
     // context.commit("changesearchproducdata", data)
    },
    async show (context,formdata){
     
      const goodsshow=await request({
        url: "/stroe/showshop",//商品二级路经showlist
        method: "post",
        data: formdata
      });
     
      // context.commit("changegoodsshow",goodsshow)
}
  }

})

